package com.wtx.leetcode;

import java.util.Stack;

/**
 * @description:  leetcode 面试题03.04 化栈为队 解法2 执行用时12ms, 内存消耗37.2mb
 * @author: wtx
 * @createDate: 2020/5/24
 */
public class MyQueue2 {

    private Stack<Integer> stack;
    private Stack<Integer> helpStack;

    /** Initialize your data structure here. */
    public MyQueue2() {
        stack = new Stack<>();
        helpStack = new Stack<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stack.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        peek();
        return helpStack.pop();
    }

    /** Get the front element. */
    public int peek() {

        if (helpStack.isEmpty())
            while (!stack.isEmpty())
                helpStack.push(stack.pop());

        return helpStack.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {

        return helpStack.isEmpty() && stack.isEmpty();
    }
}
